package io.github.talelin.latticy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.github.talelin.latticy.mapper.*;
import io.github.talelin.latticy.model.UserDO;
import io.github.talelin.latticy.pojo.*;
import io.github.talelin.latticy.service.OutputListService;
import io.github.talelin.latticy.vo.zty.OutputDetailVO;
import io.github.talelin.latticy.vo.zty.OutputListVO;
import io.github.talelin.latticy.vo.zty.ReplenishDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZTY
 * @since 2021-04-15
 */
@Service
@Transactional
public class OutputListServiceImpl extends ServiceImpl<OutputListMapper, OutputList> implements OutputListService {

    @Autowired
    ReplenishDetailMapper replenishDetailMapper;

    @Autowired
    ReplenishListMapper replenishListMapper;

    @Autowired
    OutputListMapper outputListMapper;

    @Autowired
    OutputDetailMapper outputDetailMapper;

    @Autowired
    ShopMapper shopMapper;

    @Autowired
    UserMapper userMapper;


    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public int createOutputListByReplenishList(int replenishListId, int handlerId) {
        ReplenishList replenishList = replenishListMapper.selectById(replenishListId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("replenish_id", replenishListId);
        List<ReplenishDetail> outputDetails = replenishDetailMapper.selectByMap(map);
        OutputList outputList = new OutputList();
        outputList.setReplenishId(replenishListId);
        outputList.setShopId(replenishList.getShopId());
        outputList.setStatus(replenishList.getStatus());
        outputList.setUserId(handlerId);
        int insert = outputListMapper.insert(outputList);

        for (ReplenishDetail replenishDetail : outputDetails) {
            OutputDetail outputDetail = new OutputDetail();
            outputDetail.setGoodsId(replenishDetail.getGoodsId());
            outputDetail.setOutputId(outputList.getOutputId());
            outputDetail.setQuantity(replenishDetail.getQuantity());
            outputDetailMapper.insert(outputDetail);
        }

        return insert;
    }

    @Override
    public List<OutputListVO> getOutputLists() {
        QueryWrapper<OutputList> outputListQueryWrapper = new QueryWrapper<>();
        outputListQueryWrapper.orderByAsc("status");
        List<OutputList> outputLists = outputListMapper.selectList(outputListQueryWrapper);
        List<OutputListVO> outputListVOs = new ArrayList<>();
        for (OutputList outputList : outputLists) {
            Shop shop = shopMapper.selectById(outputList.getShopId());
            UserDO userDO = userMapper.selectById(outputList.getUserId());
            ReplenishList replenishList = replenishListMapper.selectById(outputList.getReplenishId());
            UserDO applier = userMapper.selectById(replenishList.getUserId());
            OutputListVO outputListVO = new OutputListVO();
            outputListVO.setOutputId(outputList.getOutputId());
            outputListVO.setCreateTime(outputList.getCreateTime());
            outputListVO.setReplenishId(outputList.getReplenishId());
            outputListVO.setShopName(shop.getShopName());
            outputListVO.setUsername(userDO.getUsername());
            outputListVO.setApplierName(applier.getUsername());
            outputListVO.setReplenishCreateTime(replenishList.getCreateTime());
            if (outputList.getStatus() == 1){
                outputListVO.setStatus("待审核");
            }else if (outputList.getStatus() == 2){
                outputListVO.setStatus("已出库，待分配送货员");
            }else{
                outputListVO.setStatus("被退回");
            }
            outputListVOs.add(outputListVO);
        }
        return outputListVOs;
    }

    @Override
    public List<OutputDetailVO> getOutputDetailsByListId(int id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("output_id",id);
        List<OutputDetail> outputDetails = outputDetailMapper.selectByMap(map);
        List<OutputDetailVO> outputDetailVOs = new ArrayList<>();
        for (OutputDetail outputDetail : outputDetails) {
            OutputDetailVO outputDetailVO = new OutputDetailVO();
            Goods goods = goodsMapper.selectById(outputDetail.getGoodsId());
            outputDetailVO.setGoodsName(goods.getGoodsName());
            outputDetailVO.setQuantity(outputDetail.getQuantity());
            outputDetailVO.setOutputId(outputDetail.getOutputId());
            outputDetailVO.setGoodsPrice(goods.getGoodsPrice());
            outputDetailVO.setOutputDetailId(outputDetail.getOutputDetailId());
            outputDetailVOs.add(outputDetailVO);
        }
        return outputDetailVOs;
    }
}
